package com.fuershiye.woman.monitor.constants;

import com.fuershiye.woman.monitor.beans.IdName;
import com.fuershiye.woman.monitor.utils.BaseUtil;
import com.google.common.collect.Lists;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 上报数据的类型 1：数值(默认) 2：空格 3：暂无数据
* */
public enum ReportType {

    DATA(1, "数值"),
    EMPTY(2, "空格"),
    NO_DATA(3, "暂无数据"),
    TIME(4, "时间"),
    NO_CITY(5, "市级免报"),
    NO_COUNTRY(6, "县级免报"),
	;

    private final Integer id;
    private final String name;

    private static final Map<String,ReportType> lookupByName=new HashMap<String, ReportType>();
    private static final Map<Integer,ReportType> lookupById=new HashMap<Integer, ReportType>();
    private static final List<IdName> allIdName = Lists.newArrayList();
    private static final List<Integer> allId = Lists.newArrayList();


    static {
        for(final ReportType r: EnumSet.allOf(ReportType.class)){
            lookupById.put(r.getId(), r);
        }
    }

    static {
        for(final ReportType r: EnumSet.allOf(ReportType.class)){
            lookupByName.put(r.getName(), r);
        }
    }

    static {
        for(final ReportType r: EnumSet.allOf(ReportType.class)){
            allId.add(r.getId());
        }
    }
    static {
        for(final ReportType r: EnumSet.allOf(ReportType.class)){
            allIdName.add(new IdName(r.getId(), r.getName()));
        }
    }

    static {
    	for(final ReportType r: EnumSet.allOf(ReportType.class)){
            lookupById.put(r.getId(), r);
    	}
    }

    ReportType(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public static ReportType getById(int id){
        return lookupById.get(id);
    }
    public static ReportType getByName(String name){
        return lookupByName.get(name);
    }


    public static String getNameById(int id){
    	if(lookupById.containsKey(id)){
        	return lookupById.get(id).name;
    	}
    	return "";
    }

    public static Map<Integer,ReportType> getAll(){
    	return lookupById;
    }
    
    public static List<IdName> getAllIdName(){
    	return allIdName;
    }

    public static List<Integer> getAllId(){
        return allId;
    }

    public static Boolean containId(Integer id){
        for (Integer one:allId){
            if(one.equals(id)){
                return true;
            }
        }
        return false;
    }

    public static Boolean containId(String id){
        for (Integer one:allId){
            if(one.toString().equals(id)){
                return true;
            }
        }
        return false;
    }

    public static Integer getIdById(String id){
        if(containId(id)){
            return Integer.parseInt(id);
        }
        return 1;
    }


    public static Double getValueByType(Integer type, String str){

        if(type==1 && BaseUtil.isNumber(str)){
            return Double.parseDouble(str);
        }

        if(type==4){
            if(BaseUtil.isNumber(str)){
                return Double.parseDouble(str);
            }
            if(":".contains(str)||"：".contains(str)){
                String [] timeArray = str.split(":");
                if(timeArray.length!=2){
                    return null;
                }else if(BaseUtil.isNumber(timeArray[0]) && BaseUtil.isNumber(timeArray[1])){
                    return Double.parseDouble(timeArray[0])*60 + Double.parseDouble(timeArray[1]);
                }
            }
        }
        return null;
    }

}
